<div class="sm-rightPanel">

    <div id="list_form" class="layui-form sm-operation">

        <blockquote class="site-text layui-elem-quote">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">状态：</label>
                    <div class="layui-input-inline">
                        <select id="voteStateList" name="voteState" lay-filter="voteStateList">
                            <option value="">全部</option>
                            <option value="0">未发布</option>
                            <option value="1">已发布</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button id="query" class="layui-btn"><i class="iconfont icon-sousuo"></i>查询</button>
                </div>
            </div>
        </blockquote>

        <div class="layui-form-item">
            <button class="layui-btn layui-btn-normal" id="role_add">
                <i class="iconfont icon-add"></i>添加
            </button>
            <button class="layui-btn layui-btn-danger" id="role_del">
                <i class="iconfont icon-delete"></i>删除
            </button>
        </div>

    </div>
    <table id="list_table" class="layui-hide" lay-filter="mainList"></table>
</div>


<script>
    layui.use(['layedit','layer', 'table', 'form','layedit'], function () {
        var layedit = layui.layedit;
        var layer = layui.layer;
        var table = layui.table;
        var form = layui.form;
        var $ = layui.$;

        //表单重新渲染
        form.render();


        //表格渲染
        var tableObj = table.render({
            id: 'list_table'
            , elem: '#list_table'
            , url: '/vote/list'
            , align: "center"
            , cols: [[ //表头
                {type: 'checkbox'}
                , {type: 'numbers', title: '序号', width: 50}
                , {field: 'name', title: '活动名称', width: 200, align: 'center'}
                , {
                    field: 'voteState', title: '状态', width: 100, align: 'center', templet: function (d) {
                        return d.voteState == 0 ? '未发布' : '已发布';
                    }
                }
                ,{field: 'poster', title: '活动海报', width: 180, align: 'center',templet:function (d) {
                    return "<img class='fans-portrait' src=" + d.poster + " />"
                }}
                , {field: 'signUpStartTime', title: '报名开始时间', width: 130, align: 'center'}
                , {field: 'signUpEndTime', title: '报名截止时间', width: 130, align: 'center'}
                , {field: 'voteStartTime', title: '投票开始时间', width: 130, align: 'center'}
                , {field: 'voteEndTime', title: '投票截止时间', width: 130, align: 'center'}
                , {
                    field: 'lock', title: '操作', width: 200, templet: function (d) {
                        return d.voteState == 0 ?
                            '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="detail">修改</button>'+
                            '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="authorization">发布</button>'+
                            '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled" lay-event="viewUrl">查看链接</button>'
                            : '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="detail">修改</button>'+
                            '<button class="layui-btn layui-btn-sm layui-btn-disabled" disabled="disabled" lay-event="authorization">发布</button>'+
                            '<button class="layui-btn layui-btn-warm layui-btn-sm" lay-event="viewUrl">查看链接</button>';
                    }, unresize: true, align: 'center'
                }
            ]]
        });

        //监听工具条
        table.on('tool(mainList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'detail') {

                smartAui.dialog({
                    title: '修改'
                    , template: 'edit'
                    , saveUrl: '/vote/save'
                    , tableObj: tableObj
                    , width: 850
                    , height: 650
                    , htmlData: data
                    , success:function () {

                        // layeditIndex_brief = layedit.build('ID_vote_brief');
                        // layeditIndex_rules = layedit.build('ID_vote_rules');

                        // 设置富文本上传接口
                        layedit.set({
                            uploadImage: {
                                url: '/managerImg/uploadFile',
                                type: 'post'
                            },
                        });

                        layeditIndex_brief = layedit.build('ID_vote_brief', {
                            tool: ['strong', 'italic', 'underline', 'del'
                                ,'|'
                                ,'left', 'center', 'right'
                                ,'|'
                                ,'link', 'unlink', 'face',"code",
                                '|',
                                'image'
                            ]
                        });

                        layeditIndex_rules = layedit.build('ID_vote_rules', {
                            tool: ['strong', 'italic', 'underline', 'del'
                                ,'|'
                                ,'left', 'center', 'right'
                                ,'|'
                                ,'link', 'unlink', 'face',"code",
                                '|',
                                'image'
                            ]
                        });
                    }
                    , beforeSubmit:function (data) {
                        data.brief=layedit.getContent(layeditIndex_brief);
                        data.rules=layedit.getContent(layeditIndex_rules);

                        if(data.brief != null && data.brief != ""){

                        }else{
                            layer.msg("请填写活动简介!");
                            return false;
                        }

                        if(data.rules != null && data.rules != ""){

                        }else{
                            layer.msg("请填写活动规则!");
                            return false;
                        }

                        return true;
                    }
                })
            }else if (layEvent === 'authorization') {

                smartAui.confirm('确认发布？', function () {
                    $.ajax({
                        type: "GET",
                        url: '/vote/release',
                        data: {"id": data.id},
                        success: function (result) {
                            if (result.success) {
                                layer.msg("发布成功");
                                smartAui.reloadTable(tableObj);
                            } else {
                                layer.msg("发布异常");
                            }
                        }
                    })
                }, 0);
            }else if (layEvent === 'viewUrl') {
                smartAui.dialog({
                    title: '报名链接'
                    , template: 'viewurl'
                    , saveUrl: ''
                    , tableObj: tableObj
                    , width: 850
                    , height: 600
                    , htmlData: data
                    ,yes:function(){
                        layer.closeAll();
                    }
                })
            }

        });

        $("#role_add").click(function () {
            smartAui.dialog({
                title: '添加'
                , template: 'add'
                , saveUrl: '/vote/save'
                , tableObj: tableObj
                , width: 850
                , height: 650
                , success:function () {
                    // layeditIndex_brief = layedit.build('ID_vote_brief');
                    // layeditIndex_rules = layedit.build('ID_vote_rules');

                    // 设置富文本上传接口
                    layedit.set({
                        uploadImage: {
                            url: '/managerImg/uploadFile',
                            type: 'post'
                        },
                    });

                    layeditIndex_brief = layedit.build('ID_vote_brief', {
                        tool: ['strong', 'italic', 'underline', 'del'
                            ,'|'
                            ,'left', 'center', 'right'
                            ,'|'
                            ,'link', 'unlink', 'face',"code",
                            '|',
                            'image'
                        ]
                    });

                    layeditIndex_rules = layedit.build('ID_vote_rules', {
                        tool: ['strong', 'italic', 'underline', 'del'
                            ,'|'
                            ,'left', 'center', 'right'
                            ,'|'
                            ,'link', 'unlink', 'face',"code",
                            '|',
                            'image'
                        ]
                    });

                }
                , beforeSubmit:function (data) {
                    data.brief=layedit.getContent(layeditIndex_brief);
                    data.rules=layedit.getContent(layeditIndex_rules);

                    if (data.brief == null || data.brief == "") {
                        layer.msg("请填写活动简介!");
                        return false;
                    }

                    if (data.rules == null || data.rules == "") {
                        layer.msg("请填写活动规则!");
                        return false;
                    }
                    return true;
                }
            })
        });

        $("#role_del").click(function () {
            var data = table.checkStatus('list_table').data;
            var arr = [];
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id);
            }
            if (data.length == 0) {
                layer.msg("请至少选择一条");
                return;
            }

            smartAui.confirm('确认删除？', function () {
                $.ajax({
                    type: "GET",
                    url: '/vote/delete',
                    data: {"id": arr.join(",")},
                    success: function (result) {
                        if (result.success) {
                            layer.msg("删除成功");
                            smartAui.reloadTable(tableObj);
                        } else {
                            layer.msg("删除异常");
                        }
                    },

                })
            }, 0);
        });


        //查询数据
        $('#query').on('click', function () {
            smartAui.reloadTable(tableObj);
        });

    });


</script>